module.exports = app => {
  const express = require('express')
  const Specialty = require('../../models/Specialty')
  const Department = require(`../../models/Department`)
  const Teacher = require('../../models/Teacher')
  const Student = require('../../models/Student')
  const Class = require('../../models/Class')
  const auth=require('../../middleware/auth')

  const router2 = express.Router({
    mergeParams: true
  })
  router2.get('/Department',auth(), async (req, res) => {
    let model = await Department.find().populate('superior');
    res.send(model)
  })

  router2.get("/TeacherNumList",auth(), async (req, res) => {
    try {
      let model = await Teacher.find({}, {
        'nid': 1
      })
      res.send({
        success: true,
        list: model
      })
    } catch (error) {
      res.send({
        success: false,
        message: error
      })
    }
  })

  router2.get("/ClassNumList",auth(), async (req, res) => {
    try {
      let model = await Class.find({
        'year': req.query.year
      }, {
        'nid': 1,
        'name': 1,
        'superior': 1
      })
      res.send({
        success: true,
        list: model
      })
    } catch (error) {
      res.send({
        success: false,
        message: error
      })
    }
  })
  router2.get("/AllClassNideList",auth(), async (req, res) => {
    try {
      let model = await Teacher.find({}, {
        'nid': 1
      })
      res.send({
        success: true,
        list: model
      })
    } catch (error) {
      res.send({
        success: false,
        message: error
      })
    }
  })
  router2.get("/SpecialyNumList",auth(), async (req, res) => {
    try {
      let model = await Specialty.find({}, {
        'nid': 1,
        "name": 1
      })
      res.send({
        success: true,
        list: model
      })
    } catch (error) {
      res.send({
        success: false,
        message: error
      })
    }
  })

  router2.post('/Student/one',auth(), async (req, res) => {

    let oj = req.body;
    req.body.class = req.body.class.toString()
    let haveClass = await Class.findById(oj.class);
    let specialty = await Specialty.findById(oj.specialty)
    console.log(haveClass);
    if (!haveClass) {
      return res.json({
        success: false,
        message: '没有这个班级！'
      })
    }
    let model = await Student.find({
      'class': oj.class
    })
    console.log(model.length);

    if (model.length == 0) {
      let changeObj = {
        "一班": 101,
        '二班': 201,
        "三班": 301,
        "四班": 401,
        "五班": 501,
        "六班": 601,
        "七班": 701,
        "八班": 801,
        "九班": 901,
        "十班": 1001
      }
      req.body.nid = req.body.xn.slice(2) + specialty.nid + changeObj[haveClass.name.slice(-2)];
    } else {
      let originStr = model[model.length - 1].nid
      let first = originStr.slice(0, 8);
      let last = originStr.slice(8);
      req.body.nid = first + (parseInt(last) + 1);
    }
    let model2 = await Student.create(req.body)
    return res.json({
      success: true,
      message: '添加成功!'
    })
  })
  app.use('/admin/api', router2)
}